package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.SysSmsDataClass;
import com.limao.DBProvider.SysSmsProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class SysSmsCacheManagr  extends BasicModule{
	
	private static SysSmsCacheManagr instance;
	private SysSmsProvider provider;
	
    public static SysSmsCacheManagr getInstance() {
        return instance;
    }

    private Cache<String, SysSmsDataClass> SysSms_ManagerCache;
    

    public SysSmsCacheManagr() {
    	super("SysSmsCacheManagr");
    	SysSms_ManagerCache = CacheFactory.createCache("cd_SysSmsCache");
    	initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    

    public void ClearCache()
    {
    	SysSms_ManagerCache.clear();
    }  
    

    
    //**************************************************************************
 
    public String getWxShareTitle()
    {
    	SysSmsDataClass tmp=Load_SysSms(1);
    	if(tmp!=null)
    	{
    		return tmp.msgtxt;
    	}
    	else
    		return "";
    }
    
 
    public String getWxShareContent()
    {
    	SysSmsDataClass tmp=Load_SysSms(2);
    	if(tmp!=null)
    	{
    		return tmp.msgtxt;
    	}
    	else
    		return "";
    }
    
    //新鲜宝  0手机短信内容 1微信分享标题 2微信分享内容
    public SysSmsDataClass Load_SysSms(int type)
    {
    	SysSmsDataClass tmp=null;
    	ArrayList<SysSmsDataClass> list=new ArrayList<SysSmsDataClass>();
        Set set =SysSms_ManagerCache.keySet();
    	Iterator it=set.iterator();
    	while(it.hasNext()){
     		String s= (String) it.next();
     		list.add(SysSms_ManagerCache.get(s));
    	}

    	if(list.size()<=0)
    	{
    		list=provider.Load_SysSms();
    		for(SysSmsDataClass obj:list)
    		{
    			SysSms_ManagerCache.put(obj.msgid,obj);
    		}
    	}
    	
    	if(list.size()>0)
    	{
    		for(SysSmsDataClass obj:list)
    		{
    			if(CacheClass.getIntValue(obj.type)==type)
    			{
    				tmp=obj;
    				break;
    			}
    		}
     	}

    	return tmp;
    }  
    

    
    
    //**************************************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =SysSmsProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }

}

